﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Oliving.Edge.AuthHelper;
using Oliving.Edge.Common.Extensions;
using Oliving.Edge.Common.LogHelper;
using Oliving.Edge.IServices;
using Oliving.Edge.IServices.XLT;
using Oliving.Edge.Model;
using Oliving.Edge.Model.Dto.Xcx;
using Oliving.Edge.Model.Models.XLT;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Oliving.Edge.Controllers.Xcx
{
    [Authorize(Permissions.Name)]
    [ApiController]
    public class CleaningPointController : OlivingControll
    {
        #region 构造函数注入
        readonly ISysUserInfoServices _sysUserInfoServices;
        readonly PermissionRequirement _requirement;
        private ICD_CleaningPointServices _cdcleaningPointServices;
        private ICD_CleaningRecordServices _cdcleaningrecordServices;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="sysUserInfoServices">用户基础信息</param>
        public CleaningPointController(ISysUserInfoServices sysUserInfoServices, PermissionRequirement requirement, IHttpContextAccessor accessor,
            ICD_CleaningPointServices cdcleaningPointServices, ICD_CleaningRecordServices cdcleaningrecordServices) :
            base(accessor, requirement)
        {
            _sysUserInfoServices = sysUserInfoServices;
            _requirement = requirement;
            _cdcleaningPointServices = cdcleaningPointServices; _cdcleaningrecordServices = cdcleaningrecordServices;
        }
        #endregion

        #region 根据扫码的保洁点ID 查询保洁点信息  白克平 2020-9-15
        /// <summary>
        ///  根据扫码的保洁点ID 查询保洁点信息
        ///  <author>白克平</author>
        ///  <datetime>2020-9-15</datetime>
        /// </summary>
        /// <param name="cid">保洁点id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/clean/pointinfo")]
        [AllowAnonymous]
        public async Task<MessageModel<xCleaningPoint>> getCleaningInfo(string cid)
        {
            var data = new MessageModel<xCleaningPoint>();
            try
            {
                if (string.IsNullOrEmpty(cid))
                {
                    data.msg = "保洁点ID不能为空";
                    data.success = false;
                    return data;
                }
                xCleaningPoint cleaning = await _cdcleaningPointServices.QueryById(cid);
                data.msg = "成功";
                data.response = cleaning;
                data.success = true;
                return data;
            }
            catch (Exception ex)
            {
                LogLock.OutSql2Log("AOPLog", new string[] { ex.Message });
                data.msg = "系统错误，请稍后重试";
                data.success = false;
                return data;
            }
        }
        #endregion

        #region 保洁记录添加 白克平 2020-9-15
        /// <summary>
        /// 保洁记录添加
        ///  <author>白克平</author>
        ///  <datetime>2020-9-15</datetime>
        /// </summary>
        /// <param name="cdinfo">保洁点内容集合</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/clean/pointadd")]
        [AllowAnonymous]
        public async Task<MessageModel<string>> CleanInsert([FromBody] CD_CleaningRecord cdinfo)
        {
            var data = new MessageModel<string>();
            try
            {
                if (cdinfo == null)
                {
                    data.success = false;
                    data.msg = "没有接收到任何数据";
                    return data;
                }
                var clean = await _cdcleaningrecordServices.Add(cdinfo);
                if (clean > 0)
                {
                    data.success = true;
                    data.msg = "保存成功";
                }
                else
                {
                    data.success = false;
                    data.msg = "保存失败，请稍后重试";
                }
                return data;
            }
            catch (Exception ex)
            {
                LogLock.OutSql2Log("AOPLog", new string[] { ex.Message });
                data.success = false;
                data.msg = "系统错误，请稍后重试";
                return data;
            }
        }

        #endregion

        #region 非保洁人员扫码 白克平 2020-9-15
        /// <summary>
        /// 非保洁人员扫码
        ///  <author>白克平</author>
        ///  <datetime>2020-9-15</datetime>
        /// </summary>
        /// <param name="cid">保洁点id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/clean/fpointinfo")]
        [AllowAnonymous]
        public async Task<MessageModel<FBjInfo>> NoBjPeople(string cid)
        {
            var data = new MessageModel<FBjInfo>();
            try
            {
                if (string.IsNullOrEmpty(cid))
                {
                    data.msg = "保洁点ID不能为空";
                    data.success = false;
                    return data;
                }
                var list = await _cdcleaningPointServices.FQueryById(cid);
                data.response = list;
                data.msg = "成功";
                data.success = true;
                return data;
            }
            catch (Exception ex)
            {
                LogLock.OutSql2Log("AOPLog", new string[] { ex.Message });
                data.msg = "系统错误，请稍后重试";
                data.success = false;
                return data;
            }
        }
        #endregion

        #region 当前检查点的保洁记录 白克平 2020-9-15
        /// <summary>
        /// 当前检查点的保洁记录
        ///  <author>白克平</author>
        ///  <datetime>2020-9-15</datetime>
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">条数</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/clean/pagelist")]
        [AllowAnonymous]
        public async Task<MessageModel<PageModel<FBjInfo>>> GetPageList(string where, int PageIndex = 1, int PageSize = 10)
        {
            var data = new MessageModel<PageModel<FBjInfo>>();
            try
            {
                var express = QueryableExtensions.True<CD_CleaningRecord>();
                if (string.IsNullOrEmpty(where))
                {
                    express = express.And(x => x.CleaningRecordId.Contains(where)).Or(x => x.Remark.Contains(where));
                }

                var list = await _cdcleaningPointServices.GetPageList(express, PageIndex, PageSize);
                data.msg = "成功";
                data.success = true;
                data.response = list;
                return data;
            }
            catch (Exception ex)
            {
                LogLock.OutSql2Log("AOPLog", new string[] { ex.Message });
                data.msg = "系统错误，请稍后重试";
                data.success = false;
                return data;
            }
        }

        #endregion
    }
}
